package com.ruoyi.web.utils;// 该代码 OpenAI SDK 版本为 2.6.0
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {
    public static void main(String[] args) {
        OpenAIClient client = OpenAIOkHttpClient.builder()
                .apiKey("sk-759ff456ec9b4bb788061c7afc377d1f")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
                .addUserMessage("给我评价一下广西科技大学.为什么这么捞")
                .model("qwen-plus")
                .build();

        try {
            ChatCompletion chatCompletion = client.chat().completions().create(params);
            System.out.println(chatCompletion);
            // 提取回答内容（核心逻辑）
            String answer = null;
            // 判断 choices 列表是否为空
            if (chatCompletion.choices() != null && !chatCompletion.choices().isEmpty()) {
                // 取第一个回答（通常只有一个）
                ChatCompletion.Choice firstChoice = chatCompletion.choices().get(0);
                // 获取消息对象
                ChatCompletionMessage message = firstChoice.message();
                // 提取 content（即回答内容）
                if (message != null) {
                    answer = message.content().toString();
                }
            }
            System.out.println("回答内容：" + answer);
        } catch (Exception e) {
            System.err.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}